package com.yswl.yeeduo.ui.task;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.widget.Toast;

import com.yswl.yeeduo.App;
import com.yswl.yeeduo.Constant;
import com.yswl.yeeduo.asynctask.BaseHttpTask;
import com.yswl.yeeduo.ui.exception.NetworkException;
import com.yswl.yeeduo.ui.http.HttpUtils;
import com.yswl.yeeduo.ui.tool.ServiceAuthManager;
import com.yswl.yeeduo.ui.tool.UrlParamsConfig;
/**
 * 注册
 * @author Administrator
 *
 */
public class RegTask extends BaseHttpTask<Map<String, String>, Void, Object> {

	public RegTask(Context context, int taskId, TaskDelegate delegate) {
		super(context,taskId, delegate);
	}

	/**
	 * 首先执行该方法，在真正的后台操作前北UI线程调用，可在该方法内执行一些准备工作，如：进度条或控件的实例化
	 */
	protected void onPreExecute() {
		super.onPreExecute();
		super.startProgressDialog();
	}
	@Override
	protected Object doInBackground(Map<String, String>... params) {
		
		String str = "";
		String url = UrlParamsConfig.getURLEncapsulation(Constant.ConfigNet.user_reg, params[0], ServiceAuthManager.getSecret(context));
		try {
			str = HttpUtils.sendGetMethod4Path(url, new HashMap<String, Object>(), "utf-8");
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			return new NetworkException();
		} catch (IOException e) {
			e.printStackTrace();
			return new NetworkException();
		}
		
		return str;
	}


	@Override
	protected void onPostExecute(Object result) {
		super.onPostExecute(result);
		stopProgressDialog();

		if (result instanceof NetworkException) {
			Toast.makeText(App.getInstance(), "网络异常", Toast.LENGTH_SHORT).show();
			return;
		}

		JSONObject json = null;
		try {
			json = new JSONObject(result + "");
			if (super.isSuccess(json)) {
//				UserBean user = UserBean.getUser(json.getString(DATA));
				String userId = json.getString(DATA);
				this.getDelegate().success_TaskDelegate(super.getTaskId(),userId);//
			}

		} catch (JSONException e) {
			Toast.makeText(App.getInstance(), "发现异常", Toast.LENGTH_SHORT).show();
		}

	}
	
	/**
	 * 用户取消线程时调用
	 */
	protected void onCancelled() {
		super.onCancelled();
		stopProgressDialog();
	}


}
